#include <curses.h>
#include <ts7200.h>
#include <io.h>
#include <user.h>
#include <defs.h>

void GetCursorXY(int *x, int *y) {
  int c = 0;
  Printf("\E[6n" );
  Getc( COM2 );       // ESC
  Getc( COM2 );       // [
  // {ROW};
  int yy = 0;
  while (TRUE) {
    c = Getc( COM2 );
    if (c < '0' || c > '9')
      break;
    yy = yy*10+(c-'0');
  }
  *y = yy;
  // {COLUMN}R
  int xx = 0;
  while (TRUE) {
    c = Getc( COM2 );
    if (c < '0' || c > '9')
      break;
    xx = xx*10+(c-'0');
  }
  *x = xx;
}

void SetCursorXY(int x, int y) {
  Printf("\E[%d;%dH", y, x );
}

void MoveCursorXY(int dx, int dy) { 
  if (dx < 0) {
    Printf("\E[%dD", -dx ); 
  } else if (dx > 0) {
    Printf("\E[%dC", dx );
  }
  if (dy < 0) {
    Printf("\E[%dA", -dy );
  } else if (dy > 0) {
    Printf("\E[%dB", dy );
  }
}   

void SetScreenColor(int fg, int bg) {
  Printf("\E[%d;%dm", fg, bg );
}

void SetScrollableRegion(int line1, int line2) {
  Printf("\E[%d;%dr", line1, line2);
}

void ClearRight() {
  Printf("\E[0K" );
}

void ClearLine() {
  Printf("\E[2K" );
}

void ClearDown() {
  Printf("\E[J" );
}

void ClearScreen() {
  Printf("\E[2J" );
}

void SaveCursor() {
  Printf("\E7");
}

void LoadCursor() {
  Printf("\E8");
}

void ResetScreen() {
  Printf("\Ec");
}
